// 封装的方法
var util = require('../../../utils/util.js');
// 封装的API
var api = require('../../../config/api.js');
// getApp
var app = getApp();
Page({
    data: {
        // 要上传的地址信息
        address: {
            mobile: '',
            address: '',
            trueName: '',
            province: 0,
            city: 0,
            area: 0,
            addressInfo: '',
            type: 0
        },
        // 判断是否为第一次添加地址 1是 0否
        addFirst: 0,
        // 编辑地址时 地址的id值
        addressId: 0,
        openSelectRegion: false,
        selectRegionLists: [{
                addressCode: 0,
                addressName: '省份',
                parent_id: 1,
                type: 1
            },
            {
                addressCode: 0,
                addressName: '城市',
                parent_id: 1,
                type: 2
            },
            {
                addressCode: 0,
                addressName: '区县',
                parent_id: 1,
                type: 3
            }
        ],
        // 当前省市区的状态
        regionType: 1,
        // 获取到的省市区的数据列表
        regionList: [],
        // 当省市县选择全部变为true
        selectRegionDone: false
    },
    // 输入手机号
    mobilechange(e) {
        let mobile = e.detail.value;
        let address = this.data.address;
        if (util.testMobile(mobile)) {
            address.mobile = mobile;
            this.setData({
                address: address
            });
        }
    },
    // 输入姓名
    bindinputName(event) {
        let address = this.data.address;
        address.trueName = event.detail.value;
        this.setData({
            address: address
        });
    },
    // 输入详细地址
    bindinputAddress(event) {
        let address = this.data.address;
        address.addressInfo = event.detail.value;
        this.setData({
            address: address
        });
    },
    // 是否设为默认值
    switchChange(e) {
        let status = e.detail.value;
        let type = 0;
        if (status == true) {
            type = 1;
        }
        let address = 'address.type';
        this.setData({
            [address]: type
        });
    },
    // 获取地址详情
    getAddressDetail() {
        let that = this;
        let addAddress = that.data.address;
        let checkedAddressList = that.data.selectRegionLists;
        // 获取地址详细信息
        util.requestApi(api.getAddressInfo, {
            addressID: that.data.addressId
        }).then(function (res) {
            if (res.code == 200) {
                // 要保存的要编辑的值
                addAddress.mobile = res.content.mobile;
                addAddress.address = res.content.provinceName + res.content.provinceName + res.content.areaName;
                addAddress.trueName = res.content.trueName;
                addAddress.province = res.content.province;
                addAddress.city = res.content.city;
                addAddress.area = res.content.area;
                addAddress.addressInfo = res.content.addressInfo;
                addAddress.type = res.content.type;
                checkedAddressList[0].addressCode = res.content.province;
                checkedAddressList[0].addressName = res.content.provinceName;
                checkedAddressList[1].addressCode = res.content.city;
                checkedAddressList[1].addressName = res.content.provinceName;
                checkedAddressList[2].addressCode = res.content.area;
                checkedAddressList[2].addressName = res.content.areaName;
                that.setData({
                    address: addAddress,
                    selectRegionLists: checkedAddressList,
                    regionType: 3
                })
            }
        })

        this.getRegionList(addAddress.city);
    },
    // 删除地址
    deleteAddress: function () {
        let id = this.data.addressId;
        wx.showModal({
            title: '提示',
            content: '您确定要删除么？',
            success: function (res) {
                if (res.confirm) {
                    util.requestApi(api.delAddress, {
                        addressIDS: [id]
                    }).then(function (res) {
                        if (res.code === 200) {
                            util.showErrorToast('删除成功');
                            wx.navigateBack();
                        } else {
                            util.showErrorToast(res.errmsg);
                        }
                    });
                }
            }
        })
    },
    setRegionDoneStatus() {
        let that = this;
        let doneStatus = that.data.selectRegionList.every(item => {
            return item.id != 0;
        });

        that.setData({
            selectRegionDone: doneStatus
        })

    },
    // 打开选择地址弹框
    chooseRegion() {
        let that = this;
        this.setData({
            openSelectRegion: !this.data.openSelectRegion
        });
        // 判断当前列表需不需要重新选取
        if (that.data.regionType == 1) {
            that.getRegionList();
        } else if (that.data.regionType == 2) {
            that.getRegionList(that.data.selectRegionLists[0].addressCode);
        } else if (that.data.regionType == 3) {
            that.getRegionList(that.data.selectRegionLists[1].addressCode);
        }

        // this.setRegionDoneStatus();

    },
    onLoad: function (options) {
        // 根据传入的值判断当前是编辑地址还是新增地址
        if (options.id != 0) {
            this.setData({
                addressId: options.id,
            });
            // 如果是编辑地址请求获取数据
            this.getAddressDetail();
        } else {
            this.getRegionList();
        }

    },
    // 点击选过的城市列表 重新选取
    selectRegionType(event) {
        let that = this;
        let type = event.target.dataset.type;
        let regionTypeIndex = event.target.dataset.regionTypeIndex;
        let selectRegionLists = that.data.selectRegionLists;
        //判断是否可点击
        if (type >= that.data.regionType) {
            return false;
        }
        let code = selectRegionLists[type - 2 >= 0 ? type - 2 : 0].addressCode;
        let addressInfo = that.data.address
        if (type == 1) {
            selectRegionLists[0] = {
                addressCode: 0,
                addressName: '省份',
                parent_id: 1,
                type: 1
            }
            selectRegionLists[1] = {
                    addressCode: 0,
                    addressName: '城市',
                    parent_id: 1,
                    type: 2
                },
                selectRegionLists[2] = {
                    addressCode: 0,
                    addressName: '区县',
                    parent_id: 1,
                    type: 3
                }
        }
        if (type == 2) {
            selectRegionLists[1] = {
                    addressCode: 0,
                    addressName: '城市',
                    parent_id: 1,
                    type: 2
                },
                selectRegionLists[2] = {
                    addressCode: 0,
                    addressName: '区县',
                    parent_id: 1,
                    type: 3
                }
        }
        addressInfo.addressInfo = ''
        that.setData({
            regionType: type,
            selectRegionLists: selectRegionLists,
            address: addressInfo
        })
        that.getRegionList(code);

    },
    // 根据省市区获得相应的列表
    selectRegion(event) {
        let that = this;
        let regionIndex = event.target.dataset.regionIndex;
        let parentCode = event.target.dataset.parentCode;
        let regionList = that.data.regionList;
        let selectRegionList = this.data.selectRegionLists;
        selectRegionList[that.data.regionType - 1].addressName = regionList[regionIndex].addressName;
        selectRegionList[that.data.regionType - 1].addressCode = regionList[regionIndex].addressCode;
        let t;
        let a = true;
        if (that.data.regionType < 3) {
            t = that.data.regionType + 1
            a = true
        } else {
            t = that.data.regionType
            a = false
        }
        that.setData({
            selectRegionDone: !a,
            regionType: t,
            selectRegionLists: selectRegionList
        })
        if (a) {
            that.getRegionList(parentCode)
        }


    },
    // 确认收货地址
    doneSelectRegion() {
        if (this.data.selectRegionDone == false) {
            return false;
        }
        let address = this.data.address;
        let selectRegionList = this.data.selectRegionLists;
        address.province = selectRegionList[0].addressCode.toString();
        address.city = selectRegionList[1].addressCode.toString();
        address.area = selectRegionList[2].addressCode.toString();
        address.address = selectRegionList[0].addressName + selectRegionList[1].addressName + selectRegionList[2].addressName;
        this.setData({
            address: address,
            openSelectRegion: false
        });

    },
    // 遮罩层
    cancelSelectRegion() {
        this.setData({
            openSelectRegion: false,
            regionType: this.data.regionDoneStatus ? 3 : 1
        });
    },
    // 获取省市区列表
    getRegionList(regionId) {
        let that = this;
        let regionType = that.data.regionType;
        if (regionType == 1) {
            // 请求获取省市区
            util.requestApi(api.getAreaList).then(function (res) {
                if (res.code == 200) {
                    that.setData({
                        regionList: res.content.data
                    })
                }
            })
        } else {
            // 请求获取省市区
            util.requestApi(api.getAreaList, {
                parentCode: regionId
            }).then(function (res) {
                if (res.code == 200) {
                    that.setData({
                        regionList: res.content.data
                    })
                }
            })
        }

    },
    // 保存收货地址
    saveAddress() {
        let address = this.data.address;
        if (address.trueName == '' || address.trueName == undefined) {
            util.showErrorToast('请输入姓名');
            return false;
        }
        if (address.mobile == '' || address.mobile == undefined) {
            util.showErrorToast('请输入手机号码');
            return false;
        }
        if (address.address == 0 || address.address == undefined) {
            util.showErrorToast('请输入省市区');
            return false;
        }
        if (address.addressInfo == '' || address.addressInfo == undefined) {
            util.showErrorToast('请输入详细地址');
            return false;
        }
        let that = this;
        let userID = wx.getStorageSync('userID')
        if (that.data.addressId == 0) {
            util.requestApi(api.setUserAddress, {
                userID: userID,
                trueName: address.trueName,
                mobile: address.mobile,
                address: address.address + address.addressInfo,
                province: address.province,
                city: address.city,
                area: address.area,
                addressInfo: address.addressInfo,
                type: address.type
            }).then(function (res) {
                if (res.code == 200) {
                    // 地址添加成功
                    wx.navigateBack()
                }
            });
        } else {
            util.requestApi(api.setUserAddress, {
                userID: userID,
                addressID: that.data.addressId,
                trueName: address.trueName,
                mobile: address.mobile,
                address: address.address + address.addressInfo,
                province: address.province,
                city: address.city,
                area: address.area,
                addressInfo: address.addressInfo,
                type: that.data.addFirst ? 1 : address.type
            }).then(function (res) {
                if (res.code == 200) {
                    // 地址添加成功
                    wx.navigateBack()
                }
            });
        }

    },
    // 对编辑地址或者新增地址的显示
    onShow: function () {
        let id = this.data.addressId;
        if (id > 0) {
            wx.setNavigationBarTitle({
                title: '编辑地址',
            })
        } else {
            // 如果是新增地址，查看已添加地址条数
            this.getUserAddress()
            wx.setNavigationBarTitle({
                title: '新增地址',
            })
        }
    },
    // 获取地址列表信息条数
    getUserAddress() {
        let that=this
        let userID = wx.getStorageSync('userID')
        util.requestApi(api.getUserAddress, {
            userID: userID
        }).then(function (res) {
            if (res.content.length == 0) {
                that.setData({
                    addFirst: 1
                })
            }
        })
    }
})